//
//  System.m
//  CocoaSynth
//
//  Created by Pierre DUCHENE on 09/02/10.
//  Copyright 2010 Pierre DUCHENE. All rights reserved.
//

#import "System.h"

static System* sharedSystem = nil;

@implementation System

#pragma mark -
#pragma mark Accessors
@synthesize sampleRate, sampleDuration, sampleSize, samplePosition, time;
@synthesize ADSRStartTime, sustainDuration;

- (void)setSampleRate:(double)newRate
{
	if (newRate <= kLowFrequency) {
		sampleRate = kLowFrequency;
	}
	else if (newRate <= kMiddleFrequency) {
		sampleRate = kMiddleFrequency;
	}
	else {
		sampleRate = kHightFrequency;
	}
	sampleDuration = sampleSize * (1/sampleRate);
}

- (void)setSampleSize:(NSUInteger)newSize
{
	sampleSize = newSize;
	sampleDuration = sampleSize * (1/sampleRate);
}

#pragma mark -
#pragma mark Other Methods
- (void)newSampleProduced
{
	time += sampleDuration;
}

#pragma mark -
#pragma mark Singleton Implementation
+ (System*)sharedSystem
{
	if (sharedSystem == nil) {
        sharedSystem = [[super allocWithZone:NULL] init];
    }
    return sharedSystem;
}

+ (id)allocWithZone:(NSZone *)zone
{
    return [[self sharedSystem] retain];
}

- (id)init
{
	self = [super init];
	if (self != nil) {
		sampleRate = kDefaultSampleRate;
		sampleSize = kDefaultSampleSize;
		sampleDuration = sampleSize * (1/sampleRate);
		samplePosition = 0.f;
		time = 0.f;
		ADSRStartTime = -5.f;
		sustainDuration = 0.5f;
	}
	return self;
}

- (id)copyWithZone:(NSZone *)zone
{
    return self;
}

- (id)retain
{
    return self;
}

- (NSUInteger)retainCount
{
    return NSUIntegerMax;  //denotes an object that cannot be released
}

- (void)release
{
    //do nothing
}

- (id)autorelease
{
    return self;
}


@end
